package panda.android.notebook.dialog;

import java.io.IOException;

import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class LuyinBoFDialog extends BaseDialog {

	private String path;
	private Context mContext;
	private boolean isPlay = false;

	public LuyinBoFDialog(Context context, int theme, String path) {
		super(context, theme);
		// TODO Auto-generated constructor stub
		mContext = context;
		this.path = path;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setGravity(Gravity.CENTER);

		TextView textView = new TextView(mContext);
		textView.setText(path);
		layout.addView(textView, new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));

		Button button = new Button(mContext);
		button.setText("播放");
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!isPlay) {
					start(v);

				} else {
					stop(v);

				}
			}
		});
		layout.addView(button, new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		title.setText("播放录音");
	};

	MediaPlayer mPlayer;

	private void start(final View v) {
		// TODO Auto-generated method stub
		mPlayer = new MediaPlayer();
		mPlayer.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				stop(v);
			}
		});
		try {
			// 设置要播放的文件
			mPlayer.setDataSource(path);
			mPlayer.prepare();
			// 播放之
			mPlayer.start();
			isPlay = true;
			((Button) v).setText("停止");
		} catch (IOException e) {

		}
	}

	private void stop(View v) {
		mPlayer.release();
		mPlayer = null;
		isPlay = false;
		((Button) v).setText("播放");
	}
}
